<?php
namespace Sfsn\Core\Util;

use Sfsn\Core\Exception\Exception;

/**
 * log类
 * 
 * @author shooke
 */
class Log
{

    /**
     * log目录
     * 
     * @var unknown
     */
    private static $logPath;

    /**
     * log开启状态
     * 
     * @var unknown
     */
    private static $logOn;

    /**
     * 保存log
     *
     * @param unknown $file            
     * @param unknown $message            
     * @param number $halt            
     * @return boolean
     */
    public static function write($file, $message, $halt = 0)
    {
        $logOn = self::$logOn;
        $logPath = self::$logPath;
        // 日志关闭时返回
        if (!$logOn){
            return true;
        }
        // 检查创建log目录        
        if (! self::makeDir($logPath)) {
            throw new Exception('log目录创建失败');
        }                
        // 记录log
        self::writelog($file, $message);
        //终止程序开关
        $halt && exit;
    }

    /**
     * 是否开启log记录
     *
     * @param bool $onOff            
     */
    public static function init($config = [])
    {
        self::$logOn = $config['LOG_ON'];
        self::$logPath = $config['LOG_PATH'];
    }

    /**
     * 写入log
     *
     * @param unknown $file            
     * @param unknown $message            
     */
    public static function writelog($file, $message)
    {
        //取得log文件名
        $file = self::logFile($file);
        // 获取log内容
        $message = self::logContent($message);
        
        //写入log
        error_log($message, 3, $file);
    }

    /**
     * 创建目录
     *
     * @param string $pathname            
     * @param number $mode            
     * @return bool
     */
    public static function makeDir($pathname, $mode = 0777)
    {
        if(is_dir($pathname)) return true;        
        return mkdir($pathname, $mode, true);
    }

    /**
     * 获取log文件名
     *
     * @param unknown $file            
     * @return string
     */
    public static function logFile($file)
    {
        $logPath = self::$logPath;
        $date = date('Ymd') . '_';
        $logFile = $logPath . $date . $file;
        
        return $logFile;
    }

    /**
     * 获取log内容
     *
     * @param unknown $message            
     * @return string
     */
    public static function logContent($message)
    {
        $time=date('Y-m-d H:i:s');
        $ip= self::getClientIp();        
        $url = $_SERVER['REQUEST_URI'] ? $_SERVER['REQUEST_URI'] : ($_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME']);
                
        return "{$time} | {$ip} | {$url} | {$message}\r\n";
    }
    /**
     * 获取客户端ip
     * @param string $default
     * @return Ambigous <unknown, string>
     */
    private static function getClientIp($default = '0.0.0.0')
    {
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $pos = array_search('unknown', $arr);
            if (false !== $pos)
                unset($arr[$pos]);
            $ip = trim($arr[0]);
        } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        // IP地址合法验证
        $ip = (false !== ip2long($ip)) ? $ip : $default;
        return $ip;
    }
}

?>